package com.cjg.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cjg.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.cjg.gulimall.product.dao.AttrGroupDao;
import com.cjg.gulimall.product.dao.CategoryDao;
import com.cjg.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.cjg.gulimall.product.entity.AttrGroupEntity;
import com.cjg.gulimall.product.entity.CategoryEntity;
import com.cjg.gulimall.product.service.CategoryService;
import com.cjg.gulimall.product.vo.attrResVo;
import com.cjg.gulimall.product.vo.attrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjg.common.utils.PageUtils;
import com.cjg.common.utils.Query;

import com.cjg.gulimall.product.dao.AttrDao;
import com.cjg.gulimall.product.entity.AttrEntity;
import com.cjg.gulimall.product.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    AttrAttrgroupRelationDao  relationDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    CategoryDao categoryDao;

    @Autowired
    CategoryService categoryService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void save(attrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        //保存数据
        BeanUtils.copyProperties(attr,attrEntity);
        this.save(attrEntity);
        //保存关联关系
        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        entity.setAttrGroupId(attr.getAttrGroupId());
        entity.setAttrGroupId(attrEntity.getAttrId());
        relationDao.insert(entity);


    }

    @Override
    public PageUtils querybasePage(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        if(catelogId != 0){
            wrapper.eq("catelog_id",catelogId);
        }
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                wrapper
        );
        PageUtils pageUtils = new PageUtils(page);

        List<AttrEntity> records = page.getRecords();
        List<attrResVo> resVos = records.stream().map((arrtEntity) -> {
            attrResVo attrResVo = new attrResVo();
            BeanUtils.copyProperties(arrtEntity, attrResVo);

            //设置分组名字和分类名字
            AttrAttrgroupRelationEntity entity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", arrtEntity.getAttrId()));
            if (entity != null) {

                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(entity.getAttrGroupId());
                attrResVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }
            CategoryEntity categoryEntity = categoryDao.selectById(arrtEntity.getCatelogId());
            if (categoryEntity != null) {
                attrResVo.setCatelogName(categoryEntity.getName());
            }

            return attrResVo;
        }).collect(Collectors.toList());
        pageUtils.setList(resVos);
        return pageUtils;
    }

    @Override
    public attrResVo getInfoById(Long attrId) {
        attrResVo attrResVo = new attrResVo();
        AttrEntity entity = this.getById(attrId);
        BeanUtils.copyProperties(entity,attrResVo);
        //设置分组
        //从关联表中查询 用名字查询 出对应的实体类
        AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
        if(relationEntity!=null){
            attrResVo.setAttrGroupId(relationEntity.getAttrGroupId());
            // 用查询的 id 获取整个分组的信息,在获取分组的名字封装在内部
            AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId());
            if (attrGroupEntity!=null){
                attrResVo.setGroupName(attrGroupEntity.getAttrGroupName());
            }

        }


        Long attrId1 = entity.getCatelogId();
      Long[] catelogPath = categoryService.findCatelogPath(attrId1);
        attrResVo.setCatelogPath(catelogPath);

        CategoryEntity categoryEntity = categoryDao.selectById(attrId);
        if (categoryEntity!=null){
            attrResVo.setCatelogName(categoryEntity.getName());
        }

        return attrResVo;
    }

    @Transactional
    @Override
    public void updateAttrById(attrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        this.updateById(attrEntity);
        Integer attr_id = relationDao.selectCount(new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        entity.setAttrGroupId(attr.getAttrGroupId());
        entity.setAttrId(attr.getAttrId());

        if (attr_id>0){


            relationDao.update(entity,new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id",attr.getAttrId()));
        }else {

            relationDao.insert(entity);
        }

    }

}